//index.js
//获取应用实例
const app = getApp()
var QQMapWX = require('../../libs/qqmap-wx-jssdk.js');
var qqmapsdk;

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    temp: "4",
    temperature: "-1/10℃",
    type: "晴",
    city: "北京",
    date: "2000-01-01",
    direction: "无持续风向微风级",
    future: [],
    address: ''
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    qqmapsdk = new QQMapWX({
      key: 'VYEBZ-CU4KP-JV3D6-LENPD-US7N7-2YF24'
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
    var that = this;
    wx.request({
      url: 'http://apis.juhe.cn/simpleWeather/query',
      data: {
        city: "成都",
        key: "c4732432c8e4097677b2ee6613484fde"
      },
      success: function (res) {
        const result = res.data.result
        console.log(result)
        that.setData({
          future: result.future,
          temp: result.realtime.temperature,
          temperature: result.future[0].temperature,
          type: result.future[0].weather,
          direction: result.future[0].direct,
          date: result.future[0].date,
          city: result.city
        })
        console.log(that.data)
      },
      fail: function () {
        console.log("")
      }
    })
  },
  onReady: function () { },
  onShow: function () {
    let that = this
    that.getUserLocation()
  },
  onHide: function () { },
  onUnload: function () { },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onClockMe: function () {
    wx.navigateTo({
      url: '../test/test',
    })
  },
  getUserLocation: function () {
    let that = this
    wx.getSetting({
      success: (res) => {
        console.log(res)
        console.log(res.authSetting['scope.userLocation'])
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: "请求授权当前位置",
            content: "需要获取您的地理位置，请确认授权",
            success: (res) => {
              if (res.cancel) {
                wx.showToast({
                  title: "拒绝授权",
                  icon: "none",
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: (dataAu) => {
                    if (dataAu.authSetting['scope.userLocation'] == true) {
                      wx.showToast({
                        title: "授权成功",
                        icon: "none",
                        duration: 1000
                      })
                      that.getLocation()
                    } else {
                      wx.showToast({
                        title: "授权失败",
                        icon: "none",
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          that.getLocation()
        } else {
          that.getLocation()
        }
      }
    })
  },
  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
  getLocal: function (latitude, longitude) {
    let that = this
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log(res)
        let province = res.result.ad_info.province
        let address = res.result.formatted_addresses.recommend
        that.setData({
          province: province,
          address: address,
          latitude: latitude,
          longitude: longitude
        })
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  }
})